﻿//Furie transformation
namespace Diplom.Wavelets
{
    using System;
    using Diplom.NumericArrays;

    public static class Fourier
    {
        //Transform signal
        public static Complex1DArray Transform(ref Numeric1DArray source)
        {
            int size = source.Length;
            Complex1DArray target = new Complex1DArray(size);

            double W = Math.PI * 2 / source.Length;
            for (int k = 0; k < size; k++)
            {
                Complex res = new Complex(0);
                for (int i = 0; i < source.Length; i++)
                {
                    Complex cur = new Complex(source.Items[i]);
                    Complex exp = new Complex(Math.Cos(-W * i * k), Math.Sin(-W * i * k));
                    res += (cur * exp);
                }
                target.Insert(k, res);
            }
            target.TimeMin = source.TimeMin;
            target.TimeMax = source.TimeMax;
            return target;
        }

        //Syntes signal
        public static Numeric1DArray Syntes(ref Complex1DArray source)
        {
            int size = source.Length;
            Numeric1DArray array = new Numeric1DArray(size);
            double W = Math.PI * 2 / size;
            for (int i = 0; i < size; i++)
            {
                Complex res = new Complex(0);
                for (int k = 0; k < source.Length; k++)
                {
                    Complex exp = new Complex(Math.Cos(W * i * k), Math.Sin(W * i * k));
                    res =res + (exp * source.Items[k]);
                }
                array.Insert(i, res.Real / source.Length);
            }
            array.InitCharacters();
            return array;
        }

    }
}
